/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.main;

import com.ttt.nor2.config.CfgAlarm;
import com.ttt.nor2.config.Config;
import com.ttt.nor2.db.ClassAlarm;
import com.ttt.nor2.db.DBMysql;
import com.ttt.nor2.db.HtbClassAlarm;
import com.ttt.nor2.db.HtbNcen;
import com.ttt.nor2.thread.ThSweepFillter;
import com.ttt.nor2.util.ChkArgs;
import com.ttt.nor2.util.ExceptionUtil;
import java.io.File;
import java.sql.Connection;

import java.util.Hashtable;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author anuphabm
 */
public class MainSweepFillter {

    static Logger logger = Logger.getLogger(MainSweepFillter.class);

    public static void main(String args[]) {

        ChkArgs chkargs = new ChkArgs(args);
        Properties prop = chkargs.getProps();
        PropertyConfigurator.configure(chkargs.getFileConfig());
        logger.info("Start Application Sweeper Fillter...");
        // getting from file config database properties
        String url = prop.getProperty("URL");
        String usr = prop.getProperty("USER");
        String pwd = prop.getProperty("PWD");
        String db = prop.getProperty("DBNAME");

        // Getting Application parameter from Configuration file.
        int maxThread = Integer.parseInt(prop.getProperty("MAX_THREAD", "10"));
        int loopWait = Integer.parseInt(prop.getProperty("LOOP_WAIT", "2000"));
        String pathQueue = prop.getProperty("PATH_QUEUE");
        logger.debug("path queue [" + pathQueue + "]");
        //String pathError = prop.getProperty("PATH_ERROR");
        String pathProcess = prop.getProperty("PATH_PROCESS");
        logger.debug("path queue [" + pathProcess + "]");
        // connection db
        Connection conn = DBMysql.dbConn(url, usr, pwd, db);
        logger.info("Connection db success!");

        // load class alarm name
        HtbClassAlarm htca = new HtbClassAlarm(conn);
        Hashtable ht = htca.loadData();

        // load class name name center exchange
        HtbNcen n = new HtbNcen(conn);
        Hashtable htNcen = n.loadNcen();

        while (true) {

            // list file in diretory upload
            File pq = new File(pathQueue);

            String farr[] = pq.list();
            //logger.info("all file [" + farr.length + "]");
            for (int i = 0; i < farr.length; i++) {
                //check working thread
                if (ThSweepFillter.getThWork() > maxThread) {
                    thSleep(loopWait);
                    continue;
                }
                String strBit[] = farr[i].split("-");
                ClassAlarm alarm = (ClassAlarm) ht.get(strBit[0]);
                File sf = new File(pathQueue + farr[i]);//source

                if (alarm == null) {
                    //delete file
                    sf.delete();
                    logger.info("delete file " + farr[i]);
                    continue;
                }

                //move file working process
                File df = new File(pathProcess + farr[i]);//desc
                sf.renameTo(df);
                logger.info("Move file  :[" + pathQueue + farr[i] + "] =>[" + pathProcess + farr[i] + "]");
                //set config class
                CfgAlarm cfg = new CfgAlarm();
                cfg.setClassname(alarm.getClassName());
                cfg.setcAlarm(alarm);
                cfg.setHashtable(htNcen);
                cfg.setConn(conn);
                cfg.setPathUpload(pathQueue);
                cfg.setPathProcess(pathProcess);
                cfg.setFilename(farr[i]);
                ThSweepFillter t = new ThSweepFillter(cfg);
                t.start();


            }
            // gabace collecter
            System.gc();
            // thread sleep
            thSleep(loopWait);

        }//end loop while never die



    }//end main

    private static void thSleep(int sleep) {
        try {
            Thread.sleep(sleep);
            logger.debug("Thread sleep [" + sleep + "]");
        } catch (Exception e) {
            logger.error("Exception Thread sleep " + ExceptionUtil.getStackTrace(e));
        }
    }
}
